#!/usr/bin/env python

"""
Generates a list of all exports from libmupdf.dll from the function lists
contained in the mupdf/include/* headers (only MuPDF and MuXPS are included).
"""

import os, re, util2

def generateExports(header, exclude=[]):
	if os.path.isdir(header):
		return "\n".join([generateExports(os.path.join(header, file), exclude) for file in os.listdir(header)])
	
	data = open(header, "r").read()
	data = re.sub(r"(?sm)^#ifndef NDEBUG\s.*?^#endif", "", data, 0)
	data = re.sub(r"(?sm)^#ifdef ARCH_ARM\s.*?^#endif", "", data, 0)
	data = re.sub(r"(?sm)^#ifdef FITZ_DEBUG_LOCKING\s.*?^#endif", "", data, 0)
	data = data.replace(" FZ_NORETURN;", ";")
	functions = re.findall(r"(?sm)^\w+ (?:\w+ )?\*?(\w+)\(.*?\);", data)
	return "\n".join(["\t" + name for name in functions if name not in exclude])

def collectFunctions(file):
	data = open(file, "r").read()
	return re.findall(r"(?sm)^\w+(?: \*\n|\n| \*| )((?:fz_|pdf_|xps_)\w+)\(", data)

LIBMUPDF_DEF = """\
; This file is auto-generated by gen_libmupdf.def.py

LIBRARY libmupdf
EXPORTS

; Fitz exports

%(fitz_exports)s

; MuPDF exports

%(mupdf_exports)s

; MuXPS exports

%(muxps_exports)s

; jpeg exports (required for libdjvu)

	jpeg_resync_to_restart
	jpeg_finish_decompress
	jpeg_read_scanlines
	jpeg_start_decompress
	jpeg_read_header
	jpeg_CreateDecompress
	jpeg_destroy_decompress
	jpeg_std_error

; zlib exports (required for ZipUtil, PsEngine, LzmaSimpleArchive)

	gzerror
	gzprintf
	gzopen
	gzopen_w
	gzseek
	gztell
	gzread
	gzclose
	inflateInit_
	inflateInit2_
	inflate
	inflateEnd
	deflateInit_
	deflateInit2_
	deflate
	deflateEnd
	compress
	compressBound
	crc32
"""

def main():
	util2.chdir_top()
	os.chdir("mupdf")
	
	# don't include/export doc_* functions, support for additional input/output formats and form support
	doc_exports = collectFunctions("source/fitz/document.c") + ["fz_get_annot_type"]
	more_formats = collectFunctions("source/fitz/svg-device.c") + collectFunctions("source/fitz/output-pcl.c") + collectFunctions("source/fitz/output-pwg.c")
	form_exports = collectFunctions("source/pdf/pdf-form.c") + collectFunctions("source/pdf/pdf-event.c") + collectFunctions("source/pdf/pdf-appearance.c") + ["pdf_access_submit_event", "pdf_init_ui_pointer_event"]
	misc_exports = collectFunctions("source/fitz/stream-prog.c")
	sign_exports = ["pdf_crypt_buffer", "pdf_read_pfx", "pdf_sign_signature", "pdf_signer_designated_name", "pdf_free_designated_name"]
	
	fitz_exports = generateExports("include/mupdf/fitz", doc_exports + more_formats + misc_exports)
	mupdf_exports = generateExports("include/mupdf/pdf", form_exports + sign_exports + ["pdf_open_compressed_stream"])
	muxps_exports = generateExports("include/mupdf/xps.h", ["xps_parse_solid_color_brush", "xps_print_path"])
	
	list = LIBMUPDF_DEF % locals()
	open("../src/libmupdf.def", "wb").write(list.replace("\n", "\r\n"))

if __name__ == "__main__":
	main()
